from datetime import datetime
import nonebot
import pytz
from aiocqhttp.exceptions import Error as CQHttpError

@nonebot.scheduler.scheduled_job('cron', hour = '07',minute ='00' ,second = '00')
async def _():
    bot = nonebot.get_bot()
    try:
        f=open("C:\\awesome-bot\\awesome\\plugins\\QQ.txt")
        list=f.read()
        f.close
        list=eval(list)
        for user_id in list:
            try:
                s=open('c:\\awesome-bot\\awesome\\plugins\\course\\'+str(user_id)+'.txt',encoding='utf-8')
                kb =eval(s.read())
                now = datetime.now().strftime('%Y-%m-%d')  # 获取当前日期
                now = now.replace('-', '')
                if now not in kb.keys():
                    message = "今天没有课，好好自习"
                else:
                    ulist = kb[now]
                    message = ''
                    for m in ulist:
                        message = message + '\n\n' + m
                await bot.send_private_msg(user_id=user_id,message='早上好，今日课表\n'+message)
            except FileNotFoundError:#文件不能找到的异常处理
                await bot.send_private_msg(user_id=user_id,message="没有找到课表文件，请重新导入")
    except CQHttpError:
        pass

@nonebot.scheduler.scheduled_job('cron', hour = '08',minute ='00' ,second = '00')
async def _():
    bot = nonebot.get_bot()
    try:
        day=datetime.now().day
        month=datetime.now().month
        f=open("C:\\awesome-bot\\write_data.txt",encoding='utf-8')
        m=eval(f.read())
        try:
            f2=open("C:\\awesome-bot\\awesome\\plugins\\sign_log\\"+str(month)+"."+str(day)+".txt")
            p=eval(f2.read())
        except FileNotFoundError:
            pass
        #print(m)
        for i in range(0,len(p)):
            del m[str(p[i])]
        #print(m.values())
        no_sign=""
        for j in list(m.values()):
            no_sign=no_sign+str(j)+'\n'
        #print(no_sign)
        await bot.send_private_msg(user_id=1924370941,message="已签到"+str(len(p))+"人,未签到："+no_sign)
        await bot.send_group_msg(group_id=674496686,message="本次签到已结束，已签到"+str(len(p))+"人")
    except CQHttpError:
        pass

    
@nonebot.scheduler.scheduled_job('cron', hour = '16-23',minute ='00' ,second = '00')
async def _():
    bot = nonebot.get_bot()
    try:
        day=datetime.now().day
        month=datetime.now().month
        f2=open("C:\\awesome-bot\\awesome\\plugins\\loc_log\\"+str(month)+"."+str(day)+".txt")
        p=eval(f2.read())
        f2.close()
        if len(p)!=32:
            f=open("C:\\awesome-bot\\write_data.txt",encoding='utf-8')
            m=eval(f.read())#总名单
            f2=open("C:\\awesome-bot\\awesome\\plugins\\loc_log\\"+str(month)+"."+str(day)+".txt")
            p=eval(f2.read())
            for i in range(0,len(p)):
                del m[str(p[i])]#删除已签到
            no_sign=""#未签到名单
            for j in list(m.values()):
                no_sign=no_sign+str(j)+'\n'
            no_sign_qq=list(m.keys())
            at_num=""
            for i in no_sign_qq:
                at_num=at_num+f"[CQ:at,qq={i}]"+","
            await bot.send_group_msg(group_id=674496686,message="请私聊发送位置信息！！")
            await bot.send_group_msg(group_id=674496686,message=at_num)
            await bot.send_private_msg(user_id=1055991127,message="已发送位置"+str(len(p))+"人,未发送："+no_sign)
        #f3=open("C:\\awesome-bot\\awesome\\plugins\\loc_log\\"+str(month)+"."+str(day)+"time"+".txt")
        #loca=f3.read()
        #await bot.send_private_msg(user_id=1924370941,message=str(loca))
        else:
            pass
    except CQHttpError:
        pass


